﻿using System.Collections.Generic;
using System.Drawing;
using System.Windows;
using MassPokerTool.Infrastructure;
using MassPokerTool.Infrastructure.Core.Unity.BuildTrackingExtension;
using MassPokerTool.Infrastructure.Core.Unity.LogCreationExtension;
using MassPokerTool.Infrastructure.Hooking.Data;
using MassPokerTool.Infrastructure.Hooking.Handlers;
using MassPokerTool.Infrastructure.Hooking.Target;
using MassPokerTool.Infrastructure.Logging;
using MassPokerTool.Infrastructure.Ocr;
using MassPokerTool.Infrastructure.Threading;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Unity;

namespace MassPokerTool
{
    public class  Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            var shell = Container.Resolve<IShell>();
            return shell as DependencyObject;
        }

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();

            var catalog = (ModuleCatalog)ModuleCatalog;
            catalog.AddModule(typeof(Modules.Main.MainModule));
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show();
        }

        protected override ILoggerFacade CreateLogger()
        {
            log4net.Config.XmlConfigurator.Configure();
            return new Logger(typeof(Bootstrapper));
        }

        //protected override IModuleCatalog CreateModuleCatalog()
        //{
        //    return new ConfigurationModuleCatalog();
        //}

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            RegisterTypes();
        }

        private void RegisterTypes()
        {
            Container
                .AddNewExtension<BuildTracking>()
                .AddNewExtension<LogCreation>();

            Container
                .RegisterType<IShell, Shell>(new ContainerControlledLifetimeManager())
                .RegisterType<IDispatcherService, DispatcherService>(new ContainerControlledLifetimeManager())
                .RegisterType<IHookHandler, ErrorHookHandler>(new PerThreadLifetimeManager())
                .RegisterType<IHookHandler, PokerStarsChatBitBltHookHandler>(new PerThreadLifetimeManager())
                .RegisterType<IHookHandler, PokerStarsChatBeginPaintHookHandler>(new PerThreadLifetimeManager())
                .RegisterType<IHookHandler, PokerStarsChatEndPaintHookHandler>(new PerThreadLifetimeManager())
                .RegisterType<IHookHandler, CalculatorBitBltHookHandler>(new PerThreadLifetimeManager())
                .RegisterType<IHookHandler, ScreenCaptureBitBltHookHandler>(new PerThreadLifetimeManager())
                //.RegisterType<ITextRecognizer<PokerStarsChatBitBltHookData>, PokerstarsChatRecognizer>(new PerThreadLifetimeManager())
                .RegisterType<ITextRecognizer<ScreenCaptureBitBltHookData>, ScreenCaptureChatRecognizer>(new PerThreadLifetimeManager())
                .RegisterType<ITextRecognizer<PokerStarsChatEndPaintHookData>, PokerstarsChatRecognizer>(new PerThreadLifetimeManager())
                .RegisterType<IList<ITargetApplication>, ITargetApplication[]>()
                .RegisterType<ITargetApplication, PokerstarsApplication>("PokerstarsApplication")
                .RegisterType<ITargetApplication, CalculatorApplication>("CalculatorApplication")
                .RegisterType<ITargetApplication, ScreenCaptureApplication>("ScreenCaptureApplication");
        }
    }
}
